package com.sl.au.controller;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.sl.au.entity.*;
import com.sl.au.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


/*
 * 售后投诉与建议
 */

@RestController
@RequestMapping(value = "/ui/aftersalecomplain")
public class AfterSaleComplainController extends BaseController {


	@Autowired
	AfterSaleComplainService afterSaleComplainService;

	@Autowired
	SaleAreaService saleAreaService;

	@Autowired
	OptionService  optionService;

	@Autowired
	OperateLogService operateLogService;
	
	@Autowired
	AttachmentService attachmentService;
	
//	ProductMessService productMessService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("aftersalecomplain/complainindex");
		return modelAndView;
	}

	@RequestMapping(value="getpage",method = RequestMethod.GET)
	public Map<String, Object> getServicePage(HttpServletRequest request){
		Map<String, Object> map=new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		List<AfterSaleComplain> afterSaleComplains=afterSaleComplainService.getComplain(start, length, key, null, null);
		long count=afterSaleComplainService.getRepository().getCount(key);
		List<AfterSaleComplain> afterSaleComplains2=new ArrayList<>();
		for(AfterSaleComplain afterSaleComplain:afterSaleComplains){
			String parentId=afterSaleComplain.getId();
			List<Attachment>attachments=attachmentService.getRepository().findByParentId(parentId);
			Integer attachCount=attachments.size();
			afterSaleComplain.setAttachCount(attachCount);
			afterSaleComplains2.add(afterSaleComplain);
		}
		map.put("data", afterSaleComplains2);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	//// 产品信息列表
	@RequestMapping(value="getProMess",method = RequestMethod.GET)
	public Map<String, Object> getProMess(HttpServletRequest request){
		Map<String, Object> map=new HashMap<String, Object>();
		String  complainId=request.getParameter("complainId");
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		List<ProductMess> productMess=new ArrayList<ProductMess>();
		if(StringUtils.hasText(complainId)){
			productMess=afterSaleComplainService.getProductMess(start, length, key, complainId);
		}
		long count=0;
		if(key!=null){
			count=afterSaleComplainService.getProductMessRepository().getCount(complainId, key);
		}else{
			count=afterSaleComplainService.getProductMessRepository().getCount(complainId);
		}
		map.put("data", productMess);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	////保存投诉建议
	@RequestMapping(value="saveComplain",method = RequestMethod.POST)
	public Map<String, Object> saveComplain(HttpServletRequest request,@RequestBody AfterSaleComplain afterSaleComplain){
		Map<String, Object> map = new HashMap<String, Object>();
		Account a = this.getMyAccount();
		User user = a.getUser();
		//direction.setUser(user);
		AfterSaleComplain old=afterSaleComplainService.getRepository().findOne(afterSaleComplain.getId());
		if(old!=null){
			String content  = "编辑投诉建议【" + afterSaleComplain.getContact() + "】";
			operateLogService.log(user, "编辑投诉建议", content);
		}else{
			String content  = "新增投诉建议【" + afterSaleComplain.getContact() + "】";
			operateLogService.log(user, "新增投诉建议", content);
		}
		afterSaleComplain.setUser(user);
		afterSaleComplainService.getRepository().save(afterSaleComplain);
		String complainId=afterSaleComplain.getId();
		Date createTime = afterSaleComplain.getCreateTime();
		map.put("code", 0);
		map.put("complainId", complainId);
		map.put("createTime", createTime);
		return map;
	}

	/////保存产品信息
	@RequestMapping(value="saveProMess",method = RequestMethod.POST)
	public Map<String, Object> saveProMess(HttpServletRequest request,@RequestBody ProductMess productMess){
		Map<String, Object> map = new HashMap<String, Object>();
		String id=productMess.getComplain().getId();
		Account a = this.getMyAccount();
		User user = a.getUser();
		//direction.setUser(user);
		ProductMess old=afterSaleComplainService.getProductMessRepository().findOne(productMess.getId());
		if(old!=null){
			String content  = "编辑产品信息【" + productMess.getTypeSpecification() + "】";
			operateLogService.log(user, "编辑产品信息", content);
		}else{
			String content  = "新增产品信息【" + productMess.getTypeSpecification() + "】";
			operateLogService.log(user, "新增产品信息", content);
		}
		AfterSaleComplain c=afterSaleComplainService.getRepository().findById(id);
		productMess.setComplain(c);
		productMess.setUser(user);
		afterSaleComplainService.getProductMessRepository().save(productMess);
		String complainId=productMess.getId();
		Date createTime = productMess.getCreateTime();
		map.put("code", 0);
		map.put("complainId", complainId);
		map.put("createTime", createTime);
		return map;
	}

	///删除投诉建议
	@RequestMapping(value = "delComplain", method = RequestMethod.GET)
	public Map<String, Object> delComplain(HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		String ids=request.getParameter("ids");
		String id[]=ids.split(",");
		List<AfterSaleComplain> afterSaleComplains=new ArrayList<AfterSaleComplain>();
		for(int i=0;i<id.length;i++){
			String afterSaleComplainId = id[i];
			AfterSaleComplain afterSaleComplain=new AfterSaleComplain();
			List<ProductMess>messes=afterSaleComplainService.getProductMessRepository().findByComplain_Id(afterSaleComplainId);
			if(!messes.isEmpty()){
				afterSaleComplainService.getProductMessRepository().deleteInBatch(messes);
			}
			List<Attachment>attachments=attachmentService.getRepository().findByParentId(afterSaleComplainId);
			if(!attachments.isEmpty()){
				attachmentService.getRepository().deleteInBatch(attachments);;
			}
			afterSaleComplain.setId(afterSaleComplainId);
			afterSaleComplains.add(afterSaleComplain);
		}
		/////插入删除日志
		Account a = this.getMyAccount();
		User user = a.getUser();
		for(AfterSaleComplain c:afterSaleComplains){
			AfterSaleComplain old=afterSaleComplainService.getRepository().findOne(c.getId());
			String content  = "删除投诉建议【" + old.getContact() + "】";
			operateLogService.log(user, "删除投诉建议", content);
		}
		afterSaleComplainService.getRepository().deleteInBatch(afterSaleComplains);
		map.put("code", 0);
		return map;
	}

	/*删除产品信息
	 *
	 */
	@RequestMapping(value="delProMess",method = RequestMethod.GET)
	public Map<String, Object> delProMess(HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		String ids=request.getParameter("ids");
		String id[]=ids.split(",");
		List<ProductMess> pros=new ArrayList<ProductMess>();
		for(int i=0;i<id.length;i++){
			String proId = id[i];
			ProductMess a=new ProductMess();
			a.setId(proId);
			pros.add(a);
		}
		/////插入删除日志
		Account a = this.getMyAccount();
		User user = a.getUser();
		for(ProductMess p:pros){
			ProductMess old=afterSaleComplainService.getProductMessRepository().findOne(p.getId());
			String content  = "删除产品信息【" + old.getTypeSpecification() + "】";
			operateLogService.log(user, "删除产品信息", content);
		}
		afterSaleComplainService.getProductMessRepository().deleteInBatch(pros);
		map.put("code", 0);
		return map;
	}


	///增加投诉建议
	@RequestMapping(value="add",method = RequestMethod.GET)
	public ModelAndView complainAdd(HttpServletRequest request) {

		ModelAndView mv = this.createLayoutView("aftersalecomplain/complainadd");
		String qualityGroup="质量";
		String dateGroup="交货期";
		String afterServiceGroup="售后服务";
		mv.addObject("qualities", optionService.getRepository().findByGroup(qualityGroup));
		mv.addObject("dates", optionService.getRepository().findByGroup(dateGroup));
		mv.addObject("afterServices", optionService.getRepository().findByGroup(afterServiceGroup));
		return mv;
	}

	///编辑投诉建议
	@RequestMapping(value="edit",method = RequestMethod.GET)
	public ModelAndView complainEdit(HttpServletRequest request) {

		ModelAndView mv = this.createLayoutView("aftersalecomplain/complainedit");
		String complainId=request.getParameter("complainId");
		String qualityGroup="质量";
		String dateGroup="交货期";
		String afterServiceGroup="售后服务";
		if(complainId!=null){
			AfterSaleComplain afterSaleComplain=afterSaleComplainService.getRepository().findById(complainId);
			mv.addObject("afterSaleComplain",afterSaleComplain);
		}
		mv.addObject("qualities", optionService.getRepository().findByGroup(qualityGroup));
		mv.addObject("dates", optionService.getRepository().findByGroup(dateGroup));
		mv.addObject("afterServices", optionService.getRepository().findByGroup(afterServiceGroup));
		return mv;
	}

}
